const info = require('../../providers/dataAccess').info;

const DataService = function () {
  const _slideShow = async (appId,categoryId) => {
    let szSql = "SELECT title,image FROM app_slideshows WHERE appId = ? AND categoryId = ? ORDER BY sequence";
    let rows = await info.query(szSql,[appId,categoryId]);
    return rows;
  };

  const _navigationSection = async (appId,pageId) => {
    let szSql = "SELECT configs FROM app_page_compontents WHERE appId = ? AND pageId = ? AND compontentName = 'navigationSection'";
    let rows = await info.query(szSql,[appId,pageId]);
    if (rows && rows.length) {
       let config = JSON.parse(rows[0].configs);
       return config;
    }
    return null;
  }

  const  _richContent = async (appId,richId) => {
    let szSql = "SELECT title,richContent FROM app_richcontents WHERE id = ? AND appId = ?";
    let rows = await info.query(szSql,[richId,appId]);
    if (rows && rows.length) {
      let contentInfo = rows[0];
      return contentInfo;
    }
    return null;
  };

  const _modelData = async (appId,modelId,count) => {
    let szSql = "SELECT modelName,modelField FROM app_models WHERE appId = ? AND id = ?";
    let rows = await info.query(szSql,[appId,modelId]);
    if (rows && rows.length) {
      let model = rows[0];
      model.modelField = JSON.parse(model.modelField);
      let szDataSql = "";
      let datas;
      if (count > -1) {
        szDataSql = "SELECT md.id,sortValue,searchValue,dataValue FROM app_model_datas AS md  WHERE md.appId = ? AND md.modelId = ? LIMIT ?";
        datas = await info.query(szDataSql,[appId,modelId,count]);
      } else if (count == -1) {
        szDataSql = "SELECT md.id,sortValue,searchValue,dataValue FROM app_model_datas AS md  WHERE md.appId = ? AND md.modelId = ?";
        datas = await info.query(szDataSql,[appId,modelId]);
      }
      if (datas && datas.length) {
          for (var index in datas) {
            datas[index].dataValue = JSON.parse(datas[index].dataValue);
          }
      }
      model.list = datas;
      return model;
    }
    return null;
  };

  const _formKey = async (appId,formId) => {
    let szSql = "SELECT formName,formConfigs,formEvents FROM app_forms WHERE appId = ? AND id = ?";
    let rows = await info.query(szSql,[appId,formId]);
    if (rows && rows.length) {
      let model = rows[0];
      model.formConfigs = JSON.parse(model.formConfigs);
      model.formEvents = JSON.parse(model.formEvents);
      return model;
    }
    return null;
  };

  const _saveFormData = async (appId,formId,jsonValue) => {
    let szSql = "INSERT INTO app_form_datas(appId,formId,formData) VALUE (?,?,?)";
    let ret = await info.query(szSql,[appId,formId,jsonValue]);
    return ret.affectedRows > 0 ? true : false;
  };

  return {
    slideShow: _slideShow,
    richContent: _richContent,
    modelData: _modelData,
    navigationSection: _navigationSection,
    formKey: _formKey,
    saveFormData: _saveFormData
  }
}();

module.exports = DataService;
